package com.gz.logistics.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.logistics.R;
import com.gz.logistics.app.AccountActivity;
import com.gz.logistics.app.Define;
import com.gz.logistics.app.LoginActivity;
import com.gz.logistics.library.IOrder;
import com.gz.logistics.library.listener.CheckVersionListener;
import com.gz.logistics.library.pojo.CheckVersionInfo;
import com.gz.logistics.library.util.PreferenceModel;
import com.gz.logistics.util.NetworkUtil;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.lang.ref.WeakReference;

/**
 * 我的页面（退出程序，结算）
 */
public  class PersonFragment extends Fragment implements View.OnClickListener {
    private String TAG = PersonFragment.class.getSimpleName();
    private View mConvertView;
    private Context mContext;
    private RelativeLayout rl_outapp;//退出按钮
    private Dialog mMenudialog;
    private Button btn_confirm;
    private RelativeLayout rl_discovery_customservice;//结算
    private TextView version;//版本号
    private MyCheckVersionListener mCheckVersionListener;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mConvertView == null){
            mConvertView = inflater.inflate(R.layout.activity_personal,container,false);
            init();
            initListener();
        }

        return mConvertView;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    /**
     * 各种界面控件的点击事件
     * @param view
     */
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_outapp://点击退出重新登录
                createDialog();
                break;
            case R.id.rl_discovery_customservice://点击查看结算金额
                Intent intent = new Intent(getActivity(), AccountActivity.class);
                startActivity(intent);
                break;
            case R.id.version://检测版本更新

                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Define.ACTIVITY_CODE_ORDER_DETAIL && resultCode == Define.ACTIVITY_CODE_ORDER_DETAI_CHANGE){
            if (data != null){

            }
        }

    }
    /**
     *     初始化控件
     */
    private void init() {
        mContext = getActivity().getApplicationContext();
        rl_outapp = (RelativeLayout) mConvertView.findViewById(R.id.rl_outapp);
        rl_outapp.setOnClickListener(this);
        rl_discovery_customservice = (RelativeLayout) mConvertView.findViewById(R.id.rl_discovery_customservice);
        rl_discovery_customservice.setOnClickListener(this);
        version = (TextView) mConvertView.findViewById(R.id.version);
        version.setOnClickListener(this);
        mCheckVersionListener = new MyCheckVersionListener(this);
        IOrder.checkVersion(getActivity(), "logistics", mCheckVersionListener);

    }

    /**
     * 初始化各种监听器
     */
    private void initListener() {

    }
    //点击提交订单的弹框确认
    private void createDialog() {
        mMenudialog = new Dialog(getActivity(), R.style.DialogTheme);
        View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.login_out_dialog_layout, null);
        ((TextView) dialogView.findViewById(R.id.tv_title)).setText("确认退出？");

        btn_confirm = (Button) dialogView.findViewById(R.id.btn_confirm);
        btn_confirm.setText("确认");
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenudialog.dismiss();
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();

            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenudialog.dismiss();
            }
        });
        mMenudialog.setContentView(dialogView);
        mMenudialog.setCancelable(false);
        mMenudialog.setCanceledOnTouchOutside(false);
        mMenudialog.show();

    }


    private class MyCheckVersionListener extends CheckVersionListener {
        private PersonFragment mOuter;

        public MyCheckVersionListener(PersonFragment outer) {
            this.mOuter = outer;
        }

        public void release() {
            new WeakReference<PersonFragment>(mOuter);
        }

        @Override
        public void onResponseSuccess(CheckVersionInfo checkVersionInfo) {
            if (checkVersionInfo.isVersionLow()) {
                mOuter.showNoticeDialog(checkVersionInfo.getVersionIntro(),
                        checkVersionInfo.getDownloadUrl());
            } else {
                Toast.makeText(getActivity(), "当前已是最新版本", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onNoMoreData() {
        }

        @Override
        public void onNoNetWork() {
            Toast.makeText(getActivity(), Define.MSG_NO_NETWORK,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onOffline() {
        }

        @Override
        public void onResponseFailed() {
            Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT)
                    .show();
        }

    }
    public void showNoticeDialog(String updateMsg, final String apkUrl) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("发现新版本，是否要升级？");
        builder.setMessage(updateMsg);

        builder.setPositiveButton("升级", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                PreferenceModel.putBoolean(Define.KEY_HAS_NEW_VERSION, true);
                Uri uri = Uri.parse(apkUrl);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }

        });
        builder.setNegativeButton("以后再说",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        Dialog noticeDialog = builder.create();
        noticeDialog.setCanceledOnTouchOutside(false);
        noticeDialog.setCancelable(false);
        noticeDialog.show();
    }

}
